package com.chestnut.contentcore.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chestnut.common.log.annotation.Log;
import com.chestnut.common.log.enums.BusinessType;
import com.chestnut.common.security.web.PageRequest;
import com.chestnut.common.utils.IdUtils;
import com.chestnut.contentcore.domain.CmsPageWidget;
import com.chestnut.contentcore.service.ICmsOperationRecordService;
import com.chestnut.system.domain.CmsOperationRecord;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.chestnut.common.security.web.BaseRestController;

import com.chestnut.common.security.anno.Priv;
import com.chestnut.system.security.AdminUserType;
import lombok.RequiredArgsConstructor;
import com.chestnut.common.domain.R;

/**
 * cms操作记录Controller
 *
 * @author chestnut
 * @date 2025-05-12
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/operrecord")
public class CmsOperationRecordController extends BaseRestController {

    @Autowired
    private ICmsOperationRecordService cmsOperationRecordService;

    /**
     * 查询cms操作记录列表
     */

    @GetMapping("/list")
    public R<?> list(CmsOperationRecord cmsOperationRecord)
    {
        PageRequest pr = this.getPageRequest();
        LambdaQueryChainWrapper<CmsOperationRecord> wrapper = this.cmsOperationRecordService.lambdaQuery();
        if (cmsOperationRecord.getIp() != null) {
            wrapper.like(CmsOperationRecord::getIp,cmsOperationRecord.getIp());
        }
        if (cmsOperationRecord.getPreviousNterface() != null) {
            wrapper.like(CmsOperationRecord::getPreviousNterface,cmsOperationRecord.getPreviousNterface());
        }
        if (cmsOperationRecord.getAfterNterface() != null) {
            wrapper.like(CmsOperationRecord::getAfterNterface,cmsOperationRecord.getAfterNterface());
        }
        if (cmsOperationRecord.getNterface() != null) {
            wrapper.like(CmsOperationRecord::getNterface,cmsOperationRecord.getNterface());
        }
        wrapper.orderByDesc(CmsOperationRecord::getCreateTime);
        Page<CmsOperationRecord> page = wrapper
                .page(new Page<>(pr.getPageNumber(), pr.getPageSize(), true));

        return this.bindDataTable(page);
    }

    /**
     * 获取cms操作记录详细信息
     */

    @GetMapping(value = "/{id}")
    public R<?> getInfo(@PathVariable("id") Long id)
    {
        return R.ok(this.cmsOperationRecordService.getById(id));
    }

    /**
     * 新增cms操作记录
     */

    @Log(title = "cms操作记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<?> add(HttpServletRequest request, @RequestBody CmsOperationRecord cmsOperationRecord)
    {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        Long time=null;
        time=   cmsOperationRecord.getTime();
        cmsOperationRecord.setCreateTime(LocalDateTime.now());
        cmsOperationRecord.setIp(ip);
        cmsOperationRecord.setTime(null);
        this.cmsOperationRecordService.save(cmsOperationRecord);

//        if (cmsOperationRecord.getLastId()!=null){
//            CmsOperationRecord cmsOperationRecord1=new CmsOperationRecord();
//            cmsOperationRecord1.setId(cmsOperationRecord.getLastId());
//            cmsOperationRecord1.setTime(time);
//            this.cmsOperationRecordService.updateById(cmsOperationRecord1);
//        }

        return R.ok(cmsOperationRecord.getId());
    }

    /**
     * 修改cms操作记录
     */

    @Log(title = "cms操作记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody CmsOperationRecord cmsOperationRecord)
    {
        this.cmsOperationRecordService.updateById(cmsOperationRecord);
        return R.ok();
    }

    /**
     * 删除cms操作记录
     */

    @Log(title = "cms操作记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<?> remove(@PathVariable Long[] ids)
    {
        this.cmsOperationRecordService.removeBatchByIds(List.of(ids));
        return R.ok();
    }
}
